package Q206

import java.util.*

class Main {

}

fun main() {

}

class ListNode(var `val`: Int) {
    var next: ListNode? = null
}

//反转链表
//使用栈来做
class Solution {
    fun reverseList(head: ListNode?): ListNode? {
        var stack = Stack<ListNode>()
        var pre : ListNode? = head
        while(pre != null){
            stack.push(pre)
            pre = pre.next
        }

        if (stack.isEmpty()) {
            return null
        }

        var pop = stack.pop()
        var res = pop

        while(!stack.isEmpty()){
            var pop1 = stack.pop()
            pop.next = pop1
            pop = pop.next
        }
        pop.next = null
        return res
    }
}